package cn.itcast.bassm.util;

import java.io.Writer;

import cn.itcast.bassm.pojo.Person;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;



public class XstreamTest {
	
	public static void main(String[] args) {
		//创建一个Person
		Person person = new Person();
		person.setName("杨志威");
		person.setInfo("测试数据");
		
		
		//设置别名
		xstream.alias("person", Person.class);
		String xml=xstream.toXML(person);
		System.out.println(xml);
		
	}
	
	private static XStream xstream = new XStream(new XppDriver() {
		public HierarchicalStreamWriter createWriter(Writer out) {
			return new PrettyPrintWriter(out) {
				// 对所有xml节点都增加CDATA标记
				boolean cdata = true;

				public void startNode(String name, Class clazz) {
					super.startNode(name, clazz);
				}

				protected void writeText(QuickWriter writer, String text) {
					if (cdata) {
						writer.write("<![CDATA[");
						writer.write(text);
						writer.write("]]>");
					} else {
						writer.write(text);
					}
				}
			};
		}
	});

	

}
